package com.iemes.controller.hr;
/**
 * 社保增减
 */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.DockFormMap;
import com.iemes.entity.PositionFormMap;
import com.iemes.entity.SocialFormMap;
import com.iemes.entity.StaffFormMap;
import com.iemes.mapper.hr.DepartmentinfoMapper;
import com.iemes.mapper.hr.SocialMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
@Controller
@RequestMapping("/hrsocial/")
public class SocialController extends BaseController{
	
	@Inject
	private DepartmentinfoMapper departmentinfoMapper;
	@Inject
	private SocialMapper socialMapper;
	//社保增减页面
		@RequestMapping("socialAttend")
		public String examineOrder(Model model){
			DepartmentForMap departmentForMap=getFormMap(DepartmentForMap.class);
			departmentForMap.put("where", "where id!=16 ");
			//查部门名称
			List<DepartmentForMap> department=departmentinfoMapper.findByWhere(departmentForMap);
	        model.addAttribute("caset",department);
			model.addAttribute("res", findByRes());
			return Common.BACKGROUND_PATH + "/hr/social_list";
		}
		//社保增减分页
		@ResponseBody
		@RequestMapping("social_findByPage")
		public PageView findByPage(String pageNow, String pageSize) throws Exception 
		{   
			SocialFormMap socialFormMap=getFormMap(SocialFormMap.class);
			List<Map<String,Object>> mealFormMaps=socialMapper.getAllSocial(socialFormMap);
              LuogicPaging(pageNow, pageSize, mealFormMaps);
			pageView.setRecords(mealFormMaps);
			return pageView;
		}

	

		//编辑社保增减信息
		@RequestMapping("social_edit")
		public String editUI(Model model) throws Exception {
			String id = getPara("id");
			if(Common.isNotEmpty(id)){ 
				//查询社保
				SocialFormMap socialFormMap=socialMapper.findbyFrist("id", id,SocialFormMap.class);
				String social_id=socialFormMap.get("social_id").toString();
				//通过社保查询收费表
				DockFormMap dockFormMap=socialMapper.findbyFrist("social_id", social_id, DockFormMap.class);
				String peopleID=dockFormMap.get("person_id").toString();
				//通过收费表查询人员信息
				StaffFormMap staffFormMap=socialMapper.findbyFrist("id", peopleID, StaffFormMap.class);
				String positionId=staffFormMap.get("position_id").toString();
				String social_security=staffFormMap.get("social_security").toString();
				if(social_security.equals("1")){
					social_security="已购买";
				}else{
					social_security="未购买";
				}
				//通过人员信息查职务
				PositionFormMap positionFormMap=socialMapper.findbyFrist("id", positionId, PositionFormMap.class);
				String departId= positionFormMap.get("departid").toString();
				//通过职务查部门
				DepartmentForMap departmentForMap=socialMapper.findbyFrist("id", departId, DepartmentForMap.class);
				
				model.addAttribute("cocial",socialFormMap);
				model.addAttribute("dock",dockFormMap);
				model.addAttribute("staff",staffFormMap);
				model.addAttribute("social_security",social_security);
				model.addAttribute("position",positionFormMap);
				model.addAttribute("depart",departmentForMap);
			}

			return Common.BACKGROUND_PATH + "/hr/social_edit";
		}
		//修改社保增减信息
		@ResponseBody
		@RequestMapping(value="social_editEntity")
		@SystemLog(module="社保增减数据维护",methods="查看社保增减-修改社保增减")//凡需要处理业务逻辑的.都需要记录操作日志
		public String editEntity() {		
			SocialFormMap socialFormMap = getFormMap(SocialFormMap.class);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String timess=df.format(new Date());
			try {
				socialFormMap.put("record_date", timess);
				socialMapper.editEntity(socialFormMap);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "false";
			}
			return "success";
		}

		// 社保删除操作
				@ResponseBody
				@RequestMapping("social_deleteEntity")
				@SystemLog(module = "社保增减管理", methods = "社保增减管理-删除社保")
				// 凡需要处理业务逻辑的.都需要记录操作日志
				public String deletemealEntity() {


					try {
						String[] ids = getParaValues("ids");
						String Pid = null;
						for (String id : ids) {
							Pid = id;
							// orderMapper.deleteOrder(Integer.valueOf(id));
						}
						String[] value = Pid.split(",");
						for (String string : value) {
							SocialFormMap socialm=socialMapper.findbyFrist("id", string, SocialFormMap.class);
							socialMapper.deleteByAttribute("id", string, SocialFormMap.class);
								String social_id = socialm.get("social_id").toString();//得到社保id
								List<SocialFormMap> socialList = socialMapper.findByAttribute("social_id", social_id, SocialFormMap.class);
								if (socialList.isEmpty() || socialList == null) {
									DockFormMap dockcount=socialMapper.getDock(social_id);
									String id=dockcount.get("id").toString();
									DockFormMap dockFormMapq =getFormMap(DockFormMap.class);
									dockFormMapq.put("social_id", 0);
									dockFormMapq.put("id", id);
                                socialMapper.editEntity(dockFormMapq);
								}
						}
					
					} catch (Exception e) {
						// TODO Auto-generated catch block
						return "false";
					}
					return "success";
				}
			
}
